package com.csea.app.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.csea.app.model.vo.repository.RepositoryCreateReqVO;
import com.csea.app.model.vo.repository.RepositoryRspVO;
import com.csea.app.model.vo.repository.RepositoryUpdateReqVO;
import com.csea.app.service.UserRepositoryService;
import com.csea.common.aspect.ApiOperationLog;
import com.csea.common.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app/repository")
@Tag(name = "仓库管理")
public class UserRepositoryController {

    @Autowired
    private UserRepositoryService repositoryService;

    @PostMapping("/create")
    @ApiOperationLog(description = "创建仓库")
    @Operation(summary = "创建仓库")
    @SaCheckLogin
    public Response<RepositoryRspVO> create(@RequestBody @Validated RepositoryCreateReqVO reqVO) {
        return repositoryService.createRepository(reqVO);
    }

    @PostMapping("/delete/{repositoryId}")
    @ApiOperationLog(description = "删除仓库")
    @SaCheckLogin
    @Operation(summary = "删除仓库")
    public Response<String> delete(@PathVariable Integer repositoryId) {
        return repositoryService.deleteRepository(repositoryId);
    }

    @PostMapping("/edit")
    @ApiOperationLog(description = "更新仓库")
    @SaCheckLogin
    @Operation(summary = "更新仓库")
    public Response<RepositoryRspVO> update(@RequestBody @Validated RepositoryUpdateReqVO reqVO) {
        return repositoryService.updateRepository(reqVO);
    }

    @GetMapping("/list/{userId}")
    @ApiOperationLog(description = "获取用户仓库列表")
    @SaCheckLogin
    @Operation(summary = "获取用户仓库列表")
    public Response<List<RepositoryRspVO>> list(@PathVariable Integer userId) {
        return repositoryService.listUserRepositories(userId);
    }
}